<?php
/* SVN FILE: $Id: furlRedirect.php 224 2009-09-17 19:25:18Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket Friendly Urls Redirect to fix url
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 224 $
* @lastrevision $Date: 2009-09-17 19:25:18 +0000 (Thu, 17 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-17 19:25:18 +0000 (Thu, 17 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/extensions/furlRedirect.php $
*/

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class furlRedirect_ibmarket
{	
	/**
	 * Key type: Type of action (topic/forum)
	 *
	 * @access	private
	 * @var		string
	 */
	private $_type = '';
	
	/**
	 * Key ID
	 *
	 * @access	private
	 * @var		int
	 */
	private $_id = 0;
	
	/**
	* Constructor
	*
	*/
	function __construct( ipsRegistry $registry )
	{
		$this->registry =  $registry;
		$this->DB       =  $registry->DB();
		$this->settings =& $registry->fetchSettings();
	}

	/**
	 * Set the key ID
	 * @example		furlRedirect_forums::setKey( 'topic', 12 );
	 *
	 * @access	public
	 * @param	string	Type
	 * @param	mixed	Value
	 */
	public function setKey( $name, $value )
	{
		$this->_type = $name;
		$this->_id   = $value;
	}
	
	/**
	 * Set up the key by URI
	 *
	 * @access	public
	 * @param	string		URI (example: index.php?showtopic=5&view=getlastpost)
	 * @return	void
	 */
	public function setKeyByUri( $uri )
	{
		$uri = str_replace( '&amp;', '&', $uri );
		
		if ( strstr( $uri, '?' ) )
		{
			list( , $uri ) = explode( '?', $uri );
		}
		
		foreach( explode( '&', $uri ) as $bits )
		{
			list( $k, $v ) = explode( '=', $bits );
			
			if ( $k )
			{
				if ( $k == 'category' )
				{
					$this->setKey( 'shop', intval( $v ) );
					return TRUE;
				}
				else if ( $k == 'plugin' )
				{
					$this->setKey( 'plugin', $v );
					return TRUE;
				}
			}
		}
		
		return FALSE;
	}
	
	/**
	* Return the SEO title
	*
	* @access	public
	* @return	string		The SEO friendly name
	*/
	public function fetchSeoTitle()
	{
		return FALSE;
		switch ( $this->_type )
		{
			default:
				return FALSE;
			break;
			case 'shop':
				return $this->_fetchSeoTitle_category();
			break;
			case 'plugin':
				return $this->_fetchSeoTitle_plugin();
			break;
		}
	}
	
	/**
	* Return the SEO title for a category
	*
	* @access	public
	* @return	string		The SEO friendly name
	*/
	public function _fetchSeoTitle_category()
	{
		$category = $this->DB->buildAndFetch( array(	'select' => 'seo_name',
														'from'   => 'market_categories',
														 'where'  => 'cat_id=' . $this->_id ) );
		
		if ( $category['cat_id'] )
		{
			return ( $category['seo_name'] ) ? $category['seo_name'] : IPSText::makeSeoTitle( $category['cat_name'] );
		}
		
		return FALSE;
	}
	
	/**
	* Return the SEO title for a plugin
	*
	* @access	public
	* @return	string		The SEO friendly name
	*/
	public function _fetchSeoTitle_plugin()
	{
		$plugin = $this->DB->buildAndFetch( array(	'select' => '*',
													'from'   => 'market_plugins',
													'where'  => 'link=' . $this->_id ) );
		
		if ( $plugin['link'] )
		{
			return ( $plugin['seo_name'] ) ? $plugin['seo_name'] : IPSText::makeSeoTitle( $plugin['name'] );
		}
		
		return FALSE;
	}
}